package com.jiashihui.web.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName MyBatisUtil
 * @Author Administrator
 * @Date 2019-04-05 11:52
 * @Version 1.0
 **/
public class MyBatisUtil {

    public static SqlSession getSqlSession() throws IOException {
        // mybatis配置文件
        String resource = "E:\\IdeaProjects\\microservice\\web\\src\\main\\resources\\conf\\spring-mybatis.xml";
        // 得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 创建会话工厂，传入mybatis的配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 通过工厂得到SqlSession
        return sqlSessionFactory.openSession();
    }
}
